package io.gitee.crud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import io.gitee.crud.model.entity.DemoUserEntity;
import io.gitee.crud.common.PageContext;
import io.gitee.crud.service.DemoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DemoUserServiceImpl implements DemoUserService {

    @Autowired
    private IService<DemoUserEntity> service;

    @Override
    public void save(DemoUserEntity user) {
        service.save(user);
    }

    @Override
    public void delete(List<Long> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return;
        }
        List<Long> ids = userIds.stream().filter(Objects::nonNull).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        LambdaUpdateWrapper<DemoUserEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(DemoUserEntity::getIsDeleted, 1).in(DemoUserEntity::getId, ids);
        service.update(wrapper);
    }

    @Override
    public void update(DemoUserEntity user) {
        service.updateById(user);
    }

    @Override
    public IPage<DemoUserEntity> list(LocalDateTime createStart, LocalDateTime createEnd) {
        LambdaQueryWrapper<DemoUserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(DemoUserEntity::getCreateTime, createStart, createEnd);
        return service.page(PageContext.getMybatisPage(), wrapper);
    }
}
